<template>
  <div class="app-container">
    <!-- 查询模块 -->
    <div class="formDiv">
      <el-form ref="form" :inline="true" :model="form" label-width="140px">
        <el-row>
          <el-col :span="6">
            <el-form-item label="医院名称">
              <el-input v-model="form.name" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="所属分公司">
              <el-select v-loading="offLoad" filterable v-model="form.officeId" placeholder="请选择">
                <el-option
                  v-for="item in officeOption"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="医院类型">
              <!-- <el-input v-model="form.tagsId"></el-input> -->
              <el-select v-model="form.tagsId" placeholder="请选择">
                <el-option
                  v-for="item in tagOption"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="app上架状态">
              <el-select v-model="form.status" placeholder="请选择">
                <el-option
                  v-for="item in statusOption"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="app冻结状态">
              <el-select v-model="form.isAppointed" placeholder="请选择">
                <el-option
                  v-for="item in isAppointedOption"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="美学设计师上架状态">
              <!-- <el-input v-model="form.minappStatus"></el-input> -->
              <el-select v-model="form.minappStatus" placeholder="请选择">
                <el-option
                  v-for="item in minappStatusOption"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="美学设计师冻结状态">
              <el-select v-model="form.minappAppoint" placeholder="请选择">
                <el-option
                  v-for="item in minappAppointOption"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <!-- <el-form-item> -->
            <el-button style="margin-left:60px" type="primary" @click="searchFn">查询</el-button>
            <!-- <el-button v-if="hasPermission('AdminHospitalProvider.createHospital')" @click="toRoutes('hospitalAdd')" type="primary">新增</el-button> -->
            <!-- </el-form-item> -->
          </el-col>
        </el-row>
      </el-form>
    </div>
    <!-- 主页列表 -->
    <div v-loading="tableLoading" class="listDiv">
      <!-- table列表 -->
      <el-table :data="tableData" class="tableData" style="width: 100%">
        <el-table-column prop="name" label="医院名称" />
        <el-table-column prop="projectCount" label="项目数量" />
        <el-table-column prop="doctorCount" label="医生数量" />
        <el-table-column prop="tags" label="医院类型" />
        <el-table-column prop="appointmentNum" label="每日可预约次数" />
        <el-table-column prop="office" label="所属分公司" />
        <el-table-column prop="loan" label="贷款平台" />
        <el-table-column prop="star" label="评星" />
        <el-table-column prop="createdAt" label="添加时间" />
        <el-table-column label="医院隐藏">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.isShow"
              class="switchStyle"
              active-color="#13ce66"
              inactive-color="#eee"
              :active-value="1"
              :inactive-value="0"
              @click.native="changeStatus(scope.row.id,'isShow',scope.row.isShow)"
            />
          </template>
        </el-table-column>
        <el-table-column label="app上架状态">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.status"
              class="switchStyle"
              active-color="#13ce66"
              inactive-color="#eee"
              :active-value="0"
              :inactive-value="1"
              @click.native="changeStatus(scope.row.id,'status',scope.row.status)"
            />
          </template>
        </el-table-column>
        <el-table-column label="app冻结状态">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.isAppoint"
              class="switchStyle"
              active-color="#13ce66"
              inactive-color="#eee"
              :active-value="0"
              :inactive-value="1"
              @click.native="changeStatus(scope.row.id,'isAppoint',scope.row.isAppoint)"
            />
          </template>
        </el-table-column>
        <el-table-column label="美学设计师上架状态">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.minappStatus"
              class="switchStyle"
              active-color="#13ce66"
              inactive-color="#eee"
              :active-value="0"
              :inactive-value="1"
              @click.native="changeStatus(scope.row.id,'minappStatus',scope.row.minappStatus)"
            />
          </template>
        </el-table-column>
        <el-table-column label="美学设计师冻结状态">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.minappAppoint"
              class="switchStyle"
              active-color="#13ce66"
              inactive-color="#eee"
              :active-value="1"
              :inactive-value="0"
              @click.native="changeStatus(scope.row.id,'minappAppoint',scope.row.minappAppoint)"
            />
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="100">
          <template slot-scope="scope">
            <el-button
              v-if="hasPermission('AdminHospitalProvider.updateHospital')"
              type="text"
              size="small"
              @click="toRoutes('hospitalEdit',scope.row.id,'disabled')"
            >编辑</el-button>
            <el-button
              v-if="hasPermission('AdminHospitalProvider.hospitalDetail')"
              type="text"
              size="small"
              @click="toRoutes('hospitalInfo',scope.row.id)"
            >详情</el-button>
            <el-button
              v-if="hasPermission('AdminHospitalProvider.deleteHospital')"
              type="text"
              size="small"
              @click="deleteFn(scope.row.id)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="pagesCon">
        <div>
          <el-pagination
            :current-page="listForm.pageNum"
            :page-sizes="page_sizes"
            :page-size="listForm.limit"
            layout="total, sizes, prev, pager, next, jumper"
            :total="page_totals"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import {
  hospitalList,
  deleteData,
  changeHospitalStatus
} from "@/api/hospitalManage";
import { findHospitalTagsList } from "@/api/hospitalLabel";
import { companyList } from "@/api/branchOfficeDropDown";

export default {
  name: "HospitalIndex",
  filters: {
    cardType: function(e) {
      return window.helper.cardType(e);
    },
    // 0为未审核 1为审核通过  2为审核驳回
    RS: function(e) {
      switch (e) {
        case 0:
          return "未审核";
        case 1:
          return "审核通过";
        case 2:
          return "审核驳回";
        default:
          return "暂无";
      }
    }
  },
  data() {
    return {
      offLoad: true,
      tableLoading: true,
      officeOption: [],
      tagOption: [],
      statusOption: [
        {
          value: "",
          label: "全部"
        },
        {
          value: "0",
          label: "正常"
        },
        {
          value: "1",
          label: "停用"
        }
      ],
      isAppointedOption: [
        {
          value: "",
          label: "全部"
        },
        {
          value: "0",
          label: "不可预约"
        },
        {
          value: "1",
          label: "可以预约"
        }
      ],
      minappAppointOption: [
        {
          value: "",
          label: "全部"
        },
        {
          value: "0",
          label: "未冻结"
        },
        {
          value: "1",
          label: "冻结"
        }
      ],
      minappStatusOption: [
        {
          value: "",
          label: "全部"
        },
        {
          value: "0",
          label: "上架"
        },
        {
          value: "1",
          label: "下架"
        }
      ],
      form: {
        // name:"",
        officeId: "",
        tagsId: "",
        status: "",
        isAppointed: "",
        minappAppoint: "",
        minappStatus: "",
        pageNum: 1,
        pageSize: 10
      },
      tableData: [],
      page_sizes: [10, 30, 50, 100],
      page_totals: 60,
      listForm: {
        pageNum: 1,
        pageSize: 10
      }
    };
  },
  mounted() {
    // 分公司列表
    this.getCompanyList();
    if (this.$route.query.userId) {
      this.form.name = this.$route.query.userId;
    }
    this.getList(this.form);
  },
  methods: {
    // 按钮权限
    hasPermission(e) {
      return window.helper.hasPermission(e);
    },
    // 上下架与状态修改
    changeStatus(id, type, status) {
      const datas = {
        id: id + ""
      };
      datas[type] = status + "";
      changeHospitalStatus(datas).then(res => {
        // console.log(res)
        if (res.data.code === 0) {
          this.$message({
            message: "修改成功",
            type: "success"
          });
        } else {
          this.$message.error("修改失败");
        }
      });
      console.log(datas);
    },
    // 获取分公司列表
    getCompanyList() {
      companyList()
        .then(res => {
          const resData = res.data.data;
          const newData = [];
          resData.forEach(e => {
            newData.push({
              value: e.id,
              label: e.name
            });
          });
          this.officeOption = newData;
          this.officeOption.unshift({
            value: "",
            label: "全部"
          });
          this.offLoad = false;
        })
        .catch(error => {
          this.offLoad = false;
          this.$message.error(error);
        });
      // 医院类型
      const datas = {
        pageNum: 0,
        pageSize: 2000
      };
      findHospitalTagsList(datas).then(res => {
        const resData = res.data.data.list;
        const newData = [];
        resData.forEach(e => {
          newData.push({
            value: e.id,
            label: e.name
          });
        });
        this.tagOption = newData;
        this.tagOption.unshift({
          value: "",
          label: "全部"
        });
      });
    },
    // 成功状态弹窗
    succStatus(txt) {
      this.$message({
        message: txt + "成功",
        type: "success"
      });
    },
    // 失败状态弹窗
    errorStatus(txt) {
      this.$message.error(txt + "失败");
    },
    // 查询
    searchFn() {
      this.form.pageNum = 1;
      let datas = { ...this.form };
      datas.officeId = String(datas.officeId);
      datas.tagsId = String(datas.tagsId);
      this.getList(datas);
    },
    // 路由跳转
    toRoutes(e, userId, tname) {
      const route = {
        name: e,
        query: { userId: userId, disabled: tname }
      };
      this.$router.push(route);
    },
    // 列表数据获取
    getList(e) {
      this.tableLoading = true;
      const datas = e;
      hospitalList(datas)
        .then(res => {
          // console.log(res)
          if (!res.data.code) {
            // console.log(res)
            this.tableData = res.data.data.list;
            this.page_totals = res.data.data.count;
            if (this.form.officeId != "") {
              this.form.officeId = Number(this.form.officeId);
            }
            if (this.form.tagsId != "") {
              this.form.tagsId = Number(this.form.tagsId);
            }
          } else {
            this.$message.error(res.data.msg);
          }
          this.tableLoading = false;
        })
        .catch(error => {
          this.$message.error(error);
        });
    },
    // 删除
    deleteFn(e) {
      const datas = {
        id: e
      };
      this.$confirm("删除该条数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          deleteData(datas)
            .then(res => {
              console.log(res);
              if (!res.data.code) {
                this.succStatus("删除");
                this.getList(this.form);
              } else {
                this.$message.error(res.data.msg);
              }
            })
            .catch(error => {
              this.$message.error(error);
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    // 改变页面条数
    handleSizeChange(val) {
      this.form.pageSize = val;
      this.getList(this.form);
    },
    // 选择页数
    handleCurrentChange(val) {
      this.form.pageNum = val;
      let datas = { ...this.form };
      datas.officeId = String(datas.officeId);
      datas.tagsId = String(datas.tagsId);
      this.getList(datas);
    }
  }
};
</script>
<style scoped>
/* 分页 */
.pagesCon {
  height: 40px;
  margin-top: 20px;
}
.pagesCon > div {
  float: right;
}
</style>
